package com.grq.mis.validate;

import com.grq.mis.entity.Permission;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import org.springframework.util.StringUtils;

public class PermissionValidate {

    public static void add(Permission permission) {
        // name
        if (StringUtils.isEmpty(permission.getName())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限名称不能为空");
        }
        if (permission.getName().length() > 50) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限名称最多为50字");
        }

        // code
        if (StringUtils.isEmpty(permission.getCode())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限编码不能为空");
        }
        if (permission.getCode().length() > 100) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限编码最多为100字符");
        }

        // icon
        if (!StringUtils.isEmpty(permission.getIcon())) {
            if (permission.getIcon().length() > 20) {
                throw new BusinessException(ResultEnum.PARAM_ERROR, "权限图标最多为20字符");
            }
        }

        // url
        if (!StringUtils.isEmpty(permission.getUrl())) {
            if (permission.getUrl().length() > 255) {
                throw new BusinessException(ResultEnum.PARAM_ERROR, "权限连接最多为255字符");
            }
        }

        // parentId
        if (permission.getParentId() == null) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "上级权限不能为空");
        }

        // sort
        if (permission.getSort() == null) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限排序不能为空");
        }
        if (permission.getSort() < 1 || permission.getSort() > 99) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限排序范围为1-99");
        }

        // isLink
        if (permission.getIsLink() == null) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "菜单设置不能为空");
        }
    }

    public static void update(Permission permission) {
        // Id
        if (permission.getId() == null) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限ID不能为空");
        }

        // name
        if (StringUtils.isEmpty(permission.getName())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限名称不能为空");
        }
        if (permission.getName().length() > 50) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限名称最多为50字");
        }

        // code
        if (StringUtils.isEmpty(permission.getCode())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限编码不能为空");
        }
        if (permission.getCode().length() > 100) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限编码最多为100字符");
        }

        // icon
        if (!StringUtils.isEmpty(permission.getIcon())) {
            if (permission.getIcon().length() > 20) {
                throw new BusinessException(ResultEnum.PARAM_ERROR, "权限图标最多为20字符");
            }
        }

        // url
        if (!StringUtils.isEmpty(permission.getUrl())) {
            if (permission.getUrl().length() > 255) {
                throw new BusinessException(ResultEnum.PARAM_ERROR, "权限连接最多为255字符");
            }
        }

        // sort
        if (permission.getSort() == null) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限排序不能为空");
        }
        if (permission.getSort() < 1 || permission.getSort() > 99) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "权限排序范围为1-99");
        }

        // isLink
        if (permission.getIsLink() == null) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "菜单设置不能为空");
        }
    }
}
